package fag.editor;

import java.awt.Graphics;
import javax.swing.JPanel;

import fag.core.collision.Hitbox;
import fag.core.geometry.Circle2D;
import fag.core.geometry.Polygon2D;
import fag.core.geometry.Rectangle2D;
import fag.core.model.Frame;
import fag.core.model.Model;

public class DrawPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Model model;
	private int currentFrame;
	private int currentAnimation;
	private double posCenterX;
	private double posCenterY;
	
	
	public DrawPanel() {
		
		model = new Model();
		currentFrame = -1;
		currentAnimation = -1;
		
	}

	public void paintComponent(Graphics g) {
	
		if(currentFrame > -1 && currentAnimation > -1) {
			
			Frame datFrame = model.getListAnimation().get(currentAnimation).getListFrame().get(currentFrame);
			
			if(datFrame.getBufferedImage() != null) {
				
				g.drawImage(datFrame.getBufferedImage(),
							(int)(posCenterX+(datFrame.getOffSet().x)),
							(int)(posCenterY+(datFrame.getOffSet().y)),
							datFrame.getBufferedImage().getWidth(),
							datFrame.getBufferedImage().getHeight(),
							null);
			}
			
			Hitbox shape = datFrame.getHitbox();
				
					if(shape instanceof Rectangle2D) {
											
							Rectangle2D r = (Rectangle2D) shape;
							g.drawRect((int)(posCenterX+r.getX()),
									   (int)(posCenterY+r.getY()),
									   (int)r.getWidth(),
									   (int)r.getHeight());
					}	
					
					if(shape instanceof Circle2D) {
						
						Circle2D c = (Circle2D) shape;
						g.drawOval((int)(posCenterX+c.getX()-c.getRadius()),
								   (int)(posCenterY+c.getY()-c.getRadius()),
								   (int)c.getRadius()*2,
								   (int)c.getRadius()*2);
					}
					
					if(shape instanceof Polygon2D)  {
		
						Polygon2D p = (Polygon2D) shape;
						
						for(int i=0 ; i<p.getVertices().size(); i++) {
		
							if(i == (p.getVertices().size()-1)) {
								
								g.drawLine((int)(posCenterX+p.getVertices().get(i).x),
										   (int)(posCenterY+p.getVertices().get(i).y),
										   (int)(posCenterX+p.getVertices().get(0).x),
										   (int)(posCenterY+p.getVertices().get(0).y));
							}
							
							else {
							
								g.drawLine((int)(posCenterX+p.getVertices().get(i).x),
										  (int)(posCenterY+p.getVertices().get(i).y),
										  (int)(posCenterX+p.getVertices().get(i+1).x),
										  (int)(posCenterY+p.getVertices().get(i+1).y));
							}
						}
					}
		
		g.drawLine((int)posCenterX,0,(int)posCenterX,getHeight());
		g.drawLine(0,(int)posCenterY,getWidth(),(int)posCenterY);
		}
	}

	public void setModel(Model v) {
		
		model = v;
	}
	public Model getModel() {
		
		return model;
	}

	public int getCurrentAnimation() {
		
		return currentAnimation;
	}

	public void setCurrentAnimation(int i) {
		
		currentAnimation = i;
	}

	public int getCurrentFrame() {
		
		return currentFrame;
	}

	public void setCurrentFrame(int i) {
		
		currentFrame = i;
	}

	public double getPosCenterX() {
		
		return posCenterX;
	}

	public void setPosCenterX(double d) {
		
		posCenterX = d;
	}
	
	public double getPosCenterY() {
		
		return posCenterY;
	}

	public void setPosCenterY(double d) {
		
		posCenterY = d;
	}
}
